﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using Microsoft.Office.Tools.Ribbon;
using Word = Microsoft.Office.Interop.Word;

namespace Floater
{
    /// <summary>
    /// 文档：https://www.cnblogs.com/superfeeling/p/12302335.html
    /// </summary>
    public partial class ExportPdfRibbon
    {
        private void ExportPdfRibbon_Load(object sender, RibbonUIEventArgs e)
        {
            // 为PDf和XPS按钮注册点击时间 
            btnRibbonPDF.Click += new RibbonControlEventHandler(ExportDocument);
            btnRibbonXPS.Click += new RibbonControlEventHandler(ExportDocument);
            btnRibbonFont.Click += new RibbonControlEventHandler(ExportDocument);
            btnRibbonHtml.Click += new RibbonControlEventHandler(ExportDocument);
        }

        private void ExportDocument(object sender, RibbonControlEventArgs e)
        {

            switch (e.Control.Id)
            {
                // 判断点击的按钮ID 
                case "btnRibbonPDF":
                    // 打开保存文件窗口 
                    using (SaveFileDialog saveFileDialog = new SaveFileDialog())
                    {
                        // 设置保存文件窗口的相关属性 
                        saveFileDialog.Filter = "All File(*.*)|*.*";
                        saveFileDialog.DefaultExt = ".pdf";
                        saveFileDialog.RestoreDirectory = true;
                        // 在保存文件窗口中点击保存按钮 
                        if (saveFileDialog.ShowDialog() == DialogResult.OK)
                        {
                            // 导出为PDF格式 
                            Globals.ThisAddIn.Application.ActiveDocument.ExportAsFixedFormat(
                                saveFileDialog.FileName,
                                Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF);
                        }
                    }
                    break;
                case "btnRibbonXPS":
                    using (SaveFileDialog saveFileDialog2 = new SaveFileDialog())
                    {
                        saveFileDialog2.Filter = "All File(*.*)|*.*";
                        saveFileDialog2.DefaultExt = ".xps";
                        saveFileDialog2.RestoreDirectory = true;
                        if (saveFileDialog2.ShowDialog() == DialogResult.OK)
                        {
                            // 导出为XPS格式 
                            Globals.ThisAddIn.Application.ActiveDocument.ExportAsFixedFormat(
                                saveFileDialog2.FileName,
                                Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatXPS);
                        }
                    }
                    break;
                case "btnRibbonFont":

                    //定义该插入的图片是否为外部链接
                    Object linkToFile = true;               //默认,这里貌似设置为bool类型更清晰一些
                                                            //定义要插入的图片是否随Word文档一起保存
                    Object saveWithDocument = true;              //默认
                                                                 //使用InlineShapes.AddPicture方法(【即“嵌入型”】)插入图片

                    Word.Application objApplication = Globals.ThisAddIn.Application;
                    Word.Selection objSelection = objApplication.Selection;
                    Word.Range objRange = objSelection.Range;
                    Object start = objApplication.ActiveDocument.Range(objRange.Start);
                    int count = objApplication.Documents.Count;

                    int total = objRange.StoryLength;
                    int last = objRange.End;
                    if (total == objRange.End)
                        last = last - 1;

                    Object end = objApplication.ActiveDocument.Range(last);
                    objSelection.InlineShapes.AddPicture(FloaterConst.IMAGE_START_URL, ref linkToFile, ref saveWithDocument, ref start);
                    objSelection.InlineShapes.AddPicture(FloaterConst.IMAGE_ENE_URL, ref linkToFile, ref saveWithDocument, ref end);
                    objRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
                    objRange.Select();
                    break;
                case "btnRibbonHtml":
                    int pos = 0;
                    int i = 0;
                    List<Pos> posList = new List<Pos>();
                    Document doc = Globals.ThisAddIn.Application.ActiveDocument;
                    foreach (InlineShape ils in doc.InlineShapes)
                    {
                        if (ils != null)
                        {
                            i = i + 1;
                            System.Diagnostics.Debug.WriteLine(ils.Type.ToString());
                            if (ils.Type == WdInlineShapeType.wdInlineShapeLinkedPicture)
                            {
                                if (ils.LinkFormat.SourceName.Equals(FloaterConst.IMAGE_START))
                                {
                                    pos = ils.Range.Start;
                                }
                                if (ils.LinkFormat.SourceName.Equals(FloaterConst.IMAGE_ENE))
                                {
                                    Pos postion = new Pos(pos, ils.Range.Start);
                                    posList.Add(postion);
                                    pos = ils.Range.Start;
                                }
                                //ils.Select();
                                //Globals.ThisAddIn.Application.Selection.CopyAsPicture();
                                //IDataObject ido = Clipboard.GetDataObject();
                                //if (ido != null)
                                //{
                                //    if (ido.GetDataPresent(DataFormats.Bitmap))
                                //    {
                                //        Bitmap bmp = (Bitmap)ido.GetData(DataFormats.Bitmap);
                                //        string filename = @"C:\Users\Pictures\savedoc\" + i.ToString() + ".jpg";
                                //        //bmp.Save(filename, ImageFormat.Jpeg);
                                //    }
                                //}
                            }
                        }
                    }
                    foreach (Pos item in posList)
                    {
                        string guid = Guid.NewGuid().ToString();
                        Word.Range range = doc.Range(item.Start + 1, item.End);
                        range.ExportFragment($"d:\\test\\{guid}.html", Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML);
                    }
                    break;
                default:
                    return;
            }
        }
    }
}
